package net.simflow.swf.analyzer.gui.chart;

import net.simflow.swf.analyzer.gui.NetworkAnalyzerGUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.swtchart.Chart;
import org.swtchart.ISeries;
import org.swtchart.ISeriesSet;

public class ChartGUI extends Shell {
	private ToolItem tltmLogYAxis;
	private Composite composite;
	private ToolItem tltmSymbolLabel;

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public ChartGUI(Display display, String chartTitle) {
		super(display, SWT.SHELL_TRIM);
		setText(chartTitle);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setImage(SWTResourceManager.getImage(NetworkAnalyzerGUI.class,
				"/res/icon.png"));
		setSize(500, 400);
		setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));

		tltmLogYAxis = new ToolItem(toolBar, SWT.CHECK);
		tltmLogYAxis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Chart chart = (Chart) composite.getChildren()[0];
					chart.getAxisSet().getYAxis(0)
							.enableLogScale(tltmLogYAxis.getSelection());
					chart.getAxisSet().adjustRange();
					chart.redraw();
				} catch (Exception ee) {
					return;
				}
			}
		});
		tltmLogYAxis.setText("Log Y Axis");

		tltmSymbolLabel = new ToolItem(toolBar, SWT.CHECK);
		tltmSymbolLabel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Chart chart = (Chart) composite.getChildren()[0];
					ISeriesSet set = chart.getSeriesSet();
					for (ISeries s : set.getSeries()) {
						s.getLabel().setVisible(tltmSymbolLabel.getSelection());

					}
					chart.getAxisSet().adjustRange();
					chart.redraw();
				} catch (Exception ee) {
					return;
				}
			}
		});
		tltmSymbolLabel.setText("Symbol Label");

		composite = new Composite(this, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Composite getComposite() {
		return composite;
	}
}
